<?php

namespace Home\Controller;
use Think\Server;

header('content-type:text/html;charset=utf-8');
class WorkerController extends Server {

	protected $socket = 'websocket://0.0.0.0:7272';
	
	/*添加定时器
	 *监控连接状态
	 * */
	public function onWorkerStart(){
		$beginToday=strtotime('09:00:00');
		$endToday=strtotime("23:59:59");
		if(time()>$beginToday && time()<$endToday){
			F('url','http://api.abclily.com/Home/Index/getPk10?t=');
		}else{
			F('url','http://api.abclily.com/Home/Index/getXyft?t=');
		}
		$time_interval = 1;
		$url = F('url').time();
		$pkdata = json_decode(curlGet($url),true);
		$nexttime = $pkdata['next']['delayTimeInterval']+strtotime($pkdata['next']['awardTime']);
		if($nexttime-time()>8 && $nexttime-time()<300 && time()>=$beginToday && time<$endToday){
			F('state',1);
		}else{
			F('state',0);
		}
		if(!F('pk10data')){
			F('pk10data',$pkdata);
		}
		if(!F('is_send')){
			F('is_send',1);
		}
		
		/*开奖*/
		\Workerman\Lib\Timer::add($time_interval, function(){
			$beginToday=strtotime('09:00:00');
			$endToday=strtotime("23:59:59");
			$pk10data = F('pk10data');
			$next_time = $pk10data['next']['delayTimeInterval']+strtotime($pk10data['next']['awardTime']);
			if($next_time-time()>8 && $next_time-time()<300 && time()>=$beginToday && time<$endToday){
				F('state',1);
			}else{
				F('state',0);
			}
			
			if($next_time-time()==38){
				$new_message = array(
					'type' => 'admin',
					'head_img_url'=>'/Public/main/img/kefu.jpg',
					'from_client_name' => '管理员',
					'content'=>'期号:'.$pk10data['next']['periodNumber'].'<br/>'.'--距离封盘还有30秒--',
					'time'=>date('H:i:s')
				);
				foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($new_message));
				}
			}
			if($next_time-time()==8){
				F('state',0);
				F('is_send',0);
				$new_message = array(
					'type' => 'admin',
					'head_img_url'=>'/Public/main/img/kefu.jpg',
					'from_client_name' => '管理员',
					'content'=>'期号:'.$pk10data['next']['periodNumber'].'关闭，请耐心等待开奖',
					'time'=>date('H:i:s')
				);
				foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($new_message));
				}
			}
			if($next_time-time()<240 && $next_time-time()>180 && F('is_send')==0 || F('is_send')==0 && $next_time-time()>300){		
				//结算
				//开奖结果
				$current_number = M('number')->order('id DESC')->find();
				
				$number1 = explode(',', $current_number['awardnumbers']);
				$lh = unserialize($current_number['lh']);
				for($i=0;$i<count($number1);$i++){
					if($number1[$i] % 2 == 0){
						$number[$i]['ds'] = '双';
						if($number1[$i]>=6){
							$number[$i]['zuhe'] = '大双';
						}else{
							$number[$i]['zuhe'] = '小双';
						}
					}else{
						$number[$i]['ds'] = '单';
						if($number1[$i]>=6){
							$number[$i]['zuhe'] = '大单';
						}else{
							$number[$i]['zuhe'] = '小单';
						}
					}
					if($number1[$i]>=6){
						$number[$i]['dx'] = '大';
					}else{
						$number[$i]['dx'] = '小';
					}
				}	
				//当前局所有竞猜
				$list = M('order')->where("number = {$current_number['periodnumber']} && state = 1 && is_add = 0")->order("time ASC")->select();
				
				if($list){
					for($i=0;$i<count($list);$i++){
						$id = $list[$i]['id'];
						$userid = $list[$i]['userid'];
						
						//分类
						switch($list[$i]['type']){
							//车号大小单双(12345/双/100)
							case 1:
								$start1 = explode('/', $list[$i]['jincai']);
								$num1 = 0;
								$starts1 = str_split($start1[0]);
								if($start1[1]=='单' || $start1[1]=='双'){
									for($a=0;$a<count($starts1);$a++){
										if($starts1[$a]==0){
											$hao1 = '9';
										}else{
											$hao1 = $starts1[$a]-1;
										}
										if($number[$hao1]['ds']==$start1[1]){
											$num1++;
										}
									}
								}else{
									for($a=0;$a<count($starts1);$a++){
										if($starts1[$a]==0){
											$hao1 = '9';
										}else{
											$hao1 = $starts1[$a]-1;
										}
										if($number[$hao1]['dx']==$start1[1]){
											$num1++;
										}
									}
								}
								if($num1>0){
									$points1 = $num1*$start1[2]*C('dxds');
									$res1 = $this->add_points($id,$userid,$points1);
									if($res1){
										$this->send_msg('pointsadd',$points1,$userid);
									}
								} 
								break;
								
							//车号(12345/89/20)
							case 2:
								$start2 = explode('/', $list[$i]['jincai']);
								$chehao2 = str_split($start2[1]);
								$starts2 = str_split($start2[0]);
								$num2 = 0;
								for($s=0;$s<count($chehao2);$s++){
									for($a=0;$a<count($starts2);$a++){
										if($starts2[$a]==0){
											$hao2 = '9';
										}else{
											$hao2 = $starts2[$a]-1;
										}
										if($chehao2[$s]==0){
											$chehao2[$s]=10;
										}
										if($chehao2[$s]==$number1[$hao2]){
											$num2++;
										}
									}
								}
								if($num2>0){
									$points2 = $num2*$start2[2]*C('chehao');
									$res2 = $this->add_points($id,$userid,$points2);
									if($res2){
										$this->send_msg('pointsadd',$points2,$userid);
									}
								} 
								break;
								
							//组合(890/大单/50)
							case 3:
								$start3 = explode('/', $list[$i]['jincai']);
								$starts3 = str_split($start3[0]);
								$num3 = 0;
								for($a=0;$a<count($starts3);$a++){
									if($starts3[$a]==0){
										$hao3 = '9';
									}else{
										$hao3 = $starts3[$a]-1;
									}
									if($number[$hao3]['zuhe']==$start3[1]){
										$num3++;
									}
								}
								if($num3>0){
									if($start3[1]=='大单' || $start3[1]=='小双'){
										$points3 = $num3*$start3[2]*C('zuhe_1');
									}else{
										$points3 = $num3*$start3[2]*C('zuhe_2');
									}
									$res3 = $this->add_points($id,$userid,$points3);
									if($res3){
										$this->send_msg('pointsadd',$points3,$userid);
									}
								} 
								break;
								
							//龙虎(123/龙/100)
							case 4:
								$start4 = explode('/', $list[$i]['jincai']);
								$starts4 = str_split($start4[0]);
								$num4 = 0;
								for($a=0;$a<count($starts4);$a++){
									if($starts4[$a]==0){
										$hao4 = '9';
									}else{
										$hao4 = $starts4[$a]-1;
									}
									if($lh[$hao4]==$start4[1]){
										$num4++;
									}
								}
								if($num4>0){
									$points4 = $num4*$start4[2]*C('lh');
									$res4 = $this->add_points($id,$userid,$points4);
									if($res4){
										$this->send_msg('pointsadd',$points4,$userid);
									}
								} 
								break;
								
							//冠亚庄闲(庄/200)
							case 5:
								$start5 = explode('/', $list[$i]['jincai']);
								if($current_number['zx'] == $start5[0]){
									$points5 = $start5[1]*C('zx');
									$res5 = $this->add_points($id,$userid,$points5);
									if($res5){
										$this->send_msg('pointsadd',$points5,$userid);
									}
								}
								break;
								
							//冠亚号码(组/1-9.3-7/100)
							case 6:
								$start6 = explode('/', $list[$i]['jincai']);
								if(strlen($start6[1])>3){
									$zu = explode('.', $start6[1]);
									for($a=0;$a<count($zu);$a++){
										$gy = explode('-', $zu[$a]);
										if($gy[0]==0){
											$gy[0]=10;
										}
										if($gy[1]==0){
											$gy[1]=10;
										}
										if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
											$num6 = 1;
										}
									}
								}else{
									$gy = explode('-', $start6[1]);
									if($gy[0]==0){
										$gy[0]=10;
									}
									if($gy[1]==0){
										$gy[1]=10;
									}
									if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
										$num6 = 1;
									}
								}
								if($num6>0){
									$points6 = $num6*$start6[2]*C('gy');
									$res6 = $this->add_points($id,$userid,$points6);
									if($res6){
										$this->send_msg('pointsadd',$points6,$userid);
									}
								} 
								break;
								
							//特码大小单双(和双100)
							case 7:
								$start7 = substr($list[$i]['jincai'], 3,3); 
								$starts7 = substr($list[$i]['jincai'], 6);
								$num7 = 0;
								if($start7=='大' || $start7=='小'){
									if($current_number['tema_dx']==$start7){
										$num7 = 1;
									}
								}else{
									if($current_number['tema_ds']==$start7){
										$num7 = 1;
									}
								}
								if($num7>0){
									if($start7=='大' || $start7=='双'){
										$points7 = $starts7*C('tema_1');
									}else{
										$points7 = $starts7*C('tema_2');
									}
									$res7 = $this->add_points($id,$userid,$points7);
									if($res7){
										$this->send_msg('pointsadd',$points7,$userid);
									}
								}
								break;
								
							//特码数字(和5.6.7/100)
							case 8:
								$tema1 = array('03','04','18','19');
								$tema2 = array('5','6','16','17');
								$tema3 = array('7','8','14','15');
								$tema4 = array('9','10','12','13');
								$tema5 = array('11');
								
								$start8 = explode('/', $list[$i]['jincai']);
								$starts8 = substr($start8[0], 3);
								$num8 = 0;
								if(strlen($starts8)>1){
									$tlist = explode('.', $starts8);
									for($a=0;$a<count($tlist);$a++){
										if($current_number['tema']==$tlist[$a]){
											if(in_array($tlist[$a], $tema1)){
												$points8 = $start8[1]*C('tema_sz_1');
											}
											if(in_array($tlist[$a], $tema2)){
												$points8 = $start8[1]*C('tema_sz_2');
											}
											if(in_array($tlist[$a], $tema3)){
												$points8 = $start8[1]*C('tema_sz_3');
											}
											if(in_array($tlist[$a], $tema4)){
												$points8 = $start8[1]*C('tema_sz_4');
											}
											if(in_array($tlist[$a], $tema5)){
												$points8 = $start8[1]*C('tema_sz_5');
											}
											$num8 = 1;
										}
									}
								}else{
									if($current_number['tema']==$starts8){
										if(in_array($starts8, $tema1)){
											$points8 = $start8[1]*C('tema_sz_1');
										}
										if(in_array($starts8, $tema2)){
											$points8 = $start8[1]*C('tema_sz_2');
										}
										if(in_array($starts8, $tema3)){
											$points8 = $start8[1]*C('tema_sz_3');
										}
										if(in_array($starts8, $tema4)){
											$points8 = $start8[1]*C('tema_sz_4');
										}
										if(in_array($starts8, $tema5)){
											$points8 = $start8[1]*C('tema_sz_5');
										}
										$num8 = 1;
									}
								}
								if($num8>0){
									$res8 = $this->add_points($id,$userid,$points8);
									if($res8){
										$this->send_msg('pointsadd',$points8,$userid);
									}
								} 
								break;
								
							//特码区段(BC/100)
							case 9:
								$start9 = explode('/', $list[$i]['jincai']);
								$num9 = 0;
								if(strlen($start9[0])>1){
									$starts9 = str_split($start9[0]);
									for($a=0;$a<count($starts9);$a++){
										if($current_number['tema_dw']==$starts9[$a]){
											if($starts9[$a]=='A' || $starts9[$a]=='C'){
												$points9 = $start9[1]*C('tema_qd_1');
											}else{
												$points9 = $start9[1]*C('tema_qd_2');
											}
											$num9 = 1;
										}
									}
								}else{
									if($current_number['tema_dw']==$start9[0]){
										if($start9[0]=='A' || $start9[0]=='C'){
											$points9 = $start9[1]*C('tema_qd_1');
										}else{
											$points9 = $start9[1]*C('tema_qd_2');
										}
										$num9 = 1;
									}
								}
								if($num9>0 && $points9){
									$res9 = $this->add_points($id,$userid,$points9);
									if($res9){
										$this->send_msg('pointsadd',$points9,$userid);
									}
								}
								break;							
						}
					}
				}
				
				F('is_send',1);
				F('state',0);
				$new_message = array(
					'delay'=>'8',
					'type' => 'admin',
					'head_img_url'=>'/Public/main/img/kefu.jpg',
					'from_client_name' => '管理员',
					'content'=>'期号:'.$pk10data['next']['periodNumber'].'开放，祝各位中大奖',
					'time'=>date('H:i:s')
				);
				
	            foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($new_message));
				}
			}
			$this->add_message($new_message);/*添加信息*/
    	});
		
		//重载
		\Workerman\Lib\Timer::add($time_interval, function(){
			$beginToday=strtotime('09:00:00');
			$endToday=strtotime("23:59:59");
			if(time()>$beginToday && time()<$endToday){
				F('url','http://api.abclily.com/Home/Index/getPk10?t=');
			}else{
				F('url','http://api.abclily.com/Home/Index/getXyft?t=');
			}
			
			$tips = array(
				'type' => 'system',
				'head_img_url'=>'/Public/main/img/system.jpg',
				'from_client_name' => '客服',
				'content'=>'赛车即将关闭，幸运飞艇十二点开始！',
				'time'=>date('H:i:s')
			);
			if(date('H:i:s')=='23:50:00'){
				foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($tips));
				}
			}
			
			
			//动画夜场
			$new_message = array(
				'type' => 'switch',
				'time'=>date('H:i:s')
			);
			if(date('H:i:s')=='23:59:59' || date('H:i:s')=='09:00:00'){
				F('state',1);
				foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($new_message));
				}
			}
    	});
		
		//ping 统计人数
		\Workerman\Lib\Timer::add($time_interval, function(){
        	//ping客户端(获取房间内所有用户列表 )
            $clients_list = $this->worker->connections;
			$num = count($clients_list);
			$new_message = array(
				'type' => 'ping', 
				'content' => $num,
				'time' => date('H:i:s')
			);
			//if($num!=F('online')){
				//F('online',$num);
				foreach ($this->worker->connections as $conn) {
					$conn -> send(json_encode($new_message));
				}
			//}
    	});
		
		//系统公告
		\Workerman\Lib\Timer::add(300, function(){
			$new_message = array(
				'type' => 'system',
				'head_img_url'=>'/Public/main/img/system.jpg',
				'from_client_name' => '客服',
				'content'=>'由于各地网络情况不同，开奖动画仅作为参考，可能存在两秒的误差，不影响最终开奖结果！',
				'time'=>date('H:i:s')
			);
			foreach ($this->worker->connections as $conn) {
				$conn -> send(json_encode($new_message));
			}
			
    	});
		
		//存每期结果
		\Workerman\Lib\Timer::add(10, function(){
        	$url = F('url').time();
			$data = json_decode(curlGet($url),true);
			
			if(F('periodNumber')!=$data['current']['periodNumber']){
				$res = M('number')->where("periodnumber = {$data['current']['periodNumber']}")->find();
				if(!$res){
					$map['awardnumbers'] = $data['current']['awardNumbers'];
					$map['awardtime'] = $data['current']['awardTime'];
					$map['time'] = strtotime($data['current']['awardTime']);
					$map['periodnumber'] = $data['current']['periodNumber'];
					$info = explode(',', $map['awardnumbers']);
					for($i=0;$i<count($info);$i++){
						if($info[$i]<10){
							$info[$i] = substr($info[$i], 1);
						}
					}
					$map['number'] = serialize($info);
					if($info[0]>$info[9]){
						$lh[0] = '龙';
					}else{
						$lh[0] = '虎';
					}
					if($info[1]>$info[8]){
						$lh[1] = '龙';
					}else{
						$lh[1] = '虎';
					}
					if($info[2]>$info[7]){
						$lh[2] = '龙';
					}else{
						$lh[2] = '虎';
					}
					if($info[3]>$info[6]){
						$lh[3] = '龙';
					}else{
						$lh[3] = '虎';
					}
					if($info[4]>$info[5]){
						$lh[4] = '龙';
					}else{
						$lh[4] = '虎';
					}
					$map['lh'] = serialize($lh);
					$map['tema'] = $info[0]+$info[1];
					if($map['tema'] % 2 == 0){
						$map['tema_ds'] = '双';
					}else{
						$map['tema_ds'] = '单';
					}
					if($map['tema']>=12){
						$map['tema_dx'] = '大';
					}else{
						$map['tema_dx'] = '小';
					}
					if($map['tema']>=3 && $map['tema']<=7){
						$map['tema_dw'] = 'A';
					}
					if($map['tema']>=8 && $map['tema']<=14){
						$map['tema_dw'] = 'B';
					}
					if($map['tema']>=15 && $map['tema']<=19){
						$map['tema_dw'] = 'C';
					}
					if($info[0]>$info[1]){
						$map['zx'] = '庄';
					}else{
						$map['zx'] = '闲';
					}
					$res1 = M('number')->add($map);
					if($res1){
						F('pk10data',$data);
					}
				}
			}
    	});
	}
	
	/*
	 * 客户端连接时
	 * 
	 * */
	public function onConnect($connection){
		$connection->onWebSocketConnect = function($connection , $http_header)
		{
        // 可以在这里判断连接来源是否合法，不合法就关掉连接
        // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
        if($_SERVER['HTTP_ORIGIN'] != 'http://www.taoketaobao.com')
        {
            $connection->close();
        }
		};
	}
	
	/**
	 * onMessage
	 * @access public
	 * 转发客户端消息
	 * @param  void
	 * @param  void
	 * @return void
	 */
	public function onMessage($connection, $data) {
		$user = session('user');
		
		// 客户端传递的是json数据
		$message_data = json_decode($data, true);
		if (!$message_data) {
			return;
		}
				
		// 1:表示执行登陆操作 2:表示执行说话操作 3:表示执行退出操作
		// 根据类型执行不同的业务
		switch($message_data['type']){
			// 客户端回应服务端的心跳
			case 'pong' :
				break;
			case 'login' :
				// 把昵称放到session中
				$client_name = htmlspecialchars($message_data['client_name']);
				
				/* 保存uid到connection的映射，这样可以方便的通过uid查找connection，
		        * 实现针对特定uid推送数据
		        */
				$connection->uid = $message_data['client_id'];
				$this->worker->uidConnections[$connection->uid] = $connection;
				
				//session($connection->uid,$client_name);
				
				$new_message = array(
					'type' => 'admin',
					'head_img_url'=>'/Public/main/img/kefu.jpg',
					'from_client_name' => '管理员',
					'content' => '欢迎莅临北京赛车，祝您竞猜愉快！本平台由线上娱乐领航者重金打造！玩法多样，超高赔率，等你来战！', 
					'time' => date('H:i:s')
				);
				$connection -> send(json_encode($new_message));
				break;	
			case 'say' :
				$userid = $connection->uid;
				/*是否竞猜时间*/
				$state = F('state');
				if($state == 0){
					$time_error_message = array(
						'uid'  => $connection->uid,
						'type' => 'say',
						'head_img_url'=>$message_data['headimgurl'],
						'from_client_name' => $message_data['client_name'],
						'content' => $message_data['content'], 
						'time' => date('H:i:s')
					);
					$connection -> send(json_encode($time_error_message));
					$time_error_message['type'] = 'say_error'; 
					$this->add_message($time_error_message);/*添加信息*/
					
					$time_message = array(
						'uid'  => $connection->uid,
						'type' => 'admin',
						'head_img_url'=>'/Public/main/img/kefu.jpg',
						'from_client_name' => '管理员',
						'content' => '「'.$message_data['content'].'」'.'非竞猜时间，竞猜失败', 
						'time' => date('H:i:s')
					);
					$connection -> send(json_encode($time_message));
					$time_message['type'] = 'error';
					$this->add_message($time_message);/*添加信息*/
				}else{
					/*检测格式和金额*/
					$res = check_format($message_data['content']);
					if($res['error']==0){
						$error_message = array(
							'uid'=>$connection->uid,
							'type' => 'say',
							'head_img_url'=> $message_data['headimgurl'],
							'from_client_name' => $message_data['client_name'],
							'content' => $message_data['content'], 
							'time' => date('H:i:s')
						);
						$connection -> send(json_encode($error_message));
						$error_message['type'] = 'say_error';
						$this->add_message($error_message);/*添加信息*/
						
						$new_message = array(
							'uid'  => $connection->uid,
							'type' => 'admin',
							'head_img_url'=>'/Public/main/img/kefu.jpg',
							'from_client_name' => '管理员',
							'content' => '「'.$message_data['content'].'」'.'单笔点数最低20,竞猜失败', 
							'time' => date('H:i:s')
						);
						$connection -> send(json_encode($new_message));
						$new_message['type'] = 'error';
						$this->add_message($new_message);/*添加信息*/
					}else if($res['type']){
						/*查询积分*/
						$jifen = M('user')->where("id = $userid")->find();
						if($jifen['points']<$res['points']){
							$points_error = array(
								'uid'=>$connection->uid,
								'type' => 'say',
								'head_img_url'=> $message_data['headimgurl'],
								'from_client_name' => $message_data['client_name'],
								'content' => $message_data['content'], 
								'time' => date('H:i:s')
							);
							$connection -> send(json_encode($points_error));
							$points_error['type'] = 'say_error';
							$this->add_message($points_error);/*添加信息*/
							
							$points_tips = array(
								'uid'  => $connection->uid,
								'type' => 'admin',
								'head_img_url'=>'/Public/main/img/kefu.jpg',
								'from_client_name' => '管理员',
								'content' => '「'.$message_data['content'].'」'.'点数不足，竞猜失败', 
								'time' => date('H:i:s')
							);
							$connection -> send(json_encode($points_tips));
							$points_tips['type'] = 'error';
							$this->add_message($points_tips);/*添加信息*/
						}else{
							$user = M('user')->where("id = $userid")->find();
							$pk10data = F('pk10data');
							$map['userid'] = $userid;
							$map['type'] = $res['type'];
							$map['state'] = 1;
							$map['is_add'] = 0;
							$map['time'] = time();
							$map['number'] = $pk10data['next']['periodNumber'];
							$map['jincai'] = $message_data['content'];
							$map['del_points'] = $res['points'];
							$map['balance'] = $user['points']-$map['del_points'];
							$map['nickname'] = $message_data['client_name'];
							/*添加竞猜*/
							$return = $this->add_order($map);
							if($return){
								/*减分*/
								$res_points = M('user')->where("id = $userid")->setDec('points',$map['del_points']);
								
								if($res_points){
									$points_del = array(
										'type' => 'points',
										'content' => $res['points'], 
										'time' => date('H:i:s')
									);
									$connection -> send(json_encode($points_del));
								}
								
								$new_message2 = array(
									'uid'=>$connection->uid,
									'type' => 'say',
									'head_img_url'=> $message_data['headimgurl'],
									'from_client_name' => $message_data['client_name'],
									'content' => $message_data['content'], 
									'time' => date('H:i:s')
								);
								foreach ($this->worker->uidConnections as $con) {
									$con -> send(json_encode($new_message2));
								}
								$add_return = $this->add_message($new_message2);/*添加信息*/
								
								if($add_return){
									/*成功通知*/
									$new_message1 = array(
										'uid'  => $connection->uid,
										'type' => 'admin',
										'head_img_url'=>'/Public/main/img/kefu.jpg',
										'from_client_name' => '管理员',
										'content' => '「'.$message_data['content'].'」'.',竞猜成功', 
										'time' => date('H:i:s')
									);
									$connection -> send(json_encode($new_message1));
									$new_message1['type'] = 'error';
									$this->add_message($new_message1);/*添加信息*/
								}
							}
						}
					}else{
						$format_error_message = array(
							'uid'  => $connection->uid,
							'type' => 'say',
							'head_img_url'=>$message_data['headimgurl'],
							'from_client_name' => $message_data['client_name'],
							'content' => $message_data['content'], 
							'time' => date('H:i:s')
						);
						$connection -> send(json_encode($format_error_message));
						$format_error_message['type'] = 'say_error';
						$this->add_message($format_error_message);/*添加信息*/
						
						$new_message3 = array(
							'uid'  => $connection->uid,
							'type' => 'admin',
							'head_img_url'=>'/Public/main/img/kefu.jpg',
							'from_client_name' => '管理员',
							'content' => '「'.$message_data['content'].'」'.'格式不正确,竞猜失败', 
							'time' => date('H:i:s')
						);
						$connection -> send(json_encode($new_message3));
						$new_message3['type'] = 'error';
						$this->add_message($new_message3);/*添加信息*/
					}
				}
				break;
				
			case 'robot':
				if(C('robot')==1){
					$mess = $this->robot_message();
					$robot = $this->robot();
					$state = F('state');
					$new_message = array(
						'type' => 'say',
						'head_img_url'=>'/Uploads'.$robot[0]['headimgurl'],
						'from_client_name' => $robot[0]['nickname'],
						'content'=>$mess[0]['content'],
						'time'=>date('H:i:s')
					);
					if($state==1){
						if($connection->uid==1){
							foreach ($this->worker->uidConnections as $con) {
								$con -> send(json_encode($new_message));
							}
							$this->add_message($new_message);
						}
					}
			}
				break;	
		}
	}
	
	
	public function robot_message(){
		$count = M('robot_message')->count();
     	$rand = mt_rand(0,$count-1); //产生随机数。
     	$limit = $rand.','.'1'; 
    	$data = M('robot_message')->limit($limit)->select();  
		return $data;
	}
	
	public function robot(){
		$count = M('robot')->count();
     	$rand = mt_rand(0,$count-1); //产生随机数。
     	$limit = $rand.','.'1'; 
    	$data = M('robot')->limit($limit)->select();  
		return $data;
	}
	
	/**
	 * onClose
	 * 关闭连接
	 * @access public
	 * @param  void
	 * @return void
	 */
	public function onClose($connection) {
		$user = session($connection->id);
		foreach ($this->worker->uidConnections as $con) {
			if (!empty($user)) {
				$new_message = array(
					'type' => 'logout',
					'from_client_name' => $user,
					'time' => date('H:i:s')
				);
				$con -> send(json_encode($new_message));
			}
		}
		
		if(isset($connection->uid)){
	        // 连接断开时删除映射
	        unset($this->worker->uidConnections[$connection->uid]);
    	}
	}
	
	
	/*存竞猜记录和信息*/
	protected function add_order($mew_message){
		$res = M('order')->add($mew_message);
		return $res;
	}
	protected function add_message($new_message){
		$res = M('message')->add($new_message);
		return $res;
	}
	
	
	/*
	 * 竞猜成功  加分
	 * */
	public function add_points($order_id,$userid,$points){
		$res = M('user')->where("id = $userid")->setInc('points',$points);
		if($res){
			$res1 = M('order')->where("id = $order_id")->setField(array('is_add'=>'1','add_points'=>$points));
		}
		if($res && $res1){
			return 1;
		}
	}
	/*竞猜成功通知
	 * */
	public function send_msg($type,$points,$userid){
		$message_points = array(
			'type' => $type,
			'points'=>	$points,
			'time'=>date('H:i:s')
		);
		if(isset($this->worker->uidConnections[$userid])){
	        $connection = $this->worker->uidConnections[$userid];
	        $connection->send(json_encode($message_points));
	    }
	} 
	
	
	
}
?>